/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjs.liyq.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author danny
 */
public class DBUtilMySql {

    public static final String URL = "jdbc:mysql://localhost:3306/danny?characterEncoding=utf8";
    public static final String USER = "root";
    public static final String PASSWORD = "root";

    private static final ThreadLocal<Connection> connectionCache = new ThreadLocal<>();

    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }

    // 使用ThreadLocal获取Connection
//    public static synchronized Connection getConnection() {
//        Connection con = connectionCache.get();
//        if (con == null) {
//            try {
//                Class.forName("oracle.jdbc.driver.OracleDriver");
//                con = DriverManager.getConnection(URL, USER, PASSWORD);
//                connectionCache.set(con);
//            } catch (ClassNotFoundException ex) {
//                Logger.getLogger(DBUtilMySql.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (SQLException ex) {
//                Logger.getLogger(DBUtilMySql.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
//        return con;
//    }
    public static Connection getConFromDatasource() {
        Connection con = null;
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ezb");
            con = ds.getConnection();

        } catch (NamingException ex) {
            Logger.getLogger(DBUtilMySql.class
                    .getName()).log(Level.SEVERE, null, ex);

        } catch (SQLException ex) {
            Logger.getLogger(DBUtilMySql.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }

    public static void main(String[] args) throws SQLException {
        Connection con = DBUtilMySql.getConnection();
//        con.setAutoCommit(false);
        int r = con.createStatement().executeUpdate("insert into users(uname,password) values('test0917','111')");
        System.out.println(r == 1);
        con.close();
    }
}
